//
//  ListenTableViewCell.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/29.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit
protocol ListenTableViewCellDelegate:NSObjectProtocol {
    func playActionDele(cell:UITableViewCell, paly:Bool)
}

class ListenTableViewCell: UITableViewCell {
    
    var delegate:ListenTableViewCellDelegate?
    
    var onPlay:Bool = false

    @IBOutlet weak var topImageViewHeight: NSLayoutConstraint!
    @IBOutlet weak var topImageShadowHeight: NSLayoutConstraint!
    @IBOutlet weak var playBtn: UIButton!
    @IBOutlet weak var topImageView: UIImageView!
    @IBOutlet weak var topImageViewBack: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailsLabel: UILabel!
    @IBOutlet weak var finishiLabel: UILabel!
    @IBOutlet weak var hiddenView: UIView!
    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var totalTime: UILabel!
    @IBOutlet weak var currentTime: UILabel!
    @IBOutlet weak var listenNum: UILabel!
    @IBOutlet weak var toalNum: UILabel!
    @IBOutlet weak var backView: UIView!
    
    
    @IBAction func playAction(_ sender: Any) {
//        onPlay = !onPlay
//        onPlaying()
    }
    var model = ListenListModel()
    var modelList: ListenListModel? {
        didSet {
            guard let model = modelList else { return }
            self.model = model
            setUI()
            initOnPlaying()
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.topImageViewHeight.constant = SCREEN_SCALE_355X(x: 140)
        self.topImageShadowHeight.constant = SCREEN_SCALE_355X(x: 140)
    }
    
    func setUI() {
        self.backView.layer.cornerRadius = 3
        self.backView.layer.masksToBounds = true
        self.hiddenView.isHidden = true
        self.topImageView.sd_setImage(with: URL(string: StringIncludeChineseToString(self.model.imageUrl ?? "")), completed: nil)
        self.titleLabel.text = self.model.title
        self.detailsLabel.text = self.model.describe
        if self.model.status == 0 {
            self.finishiLabel.text = "（已完结）"
        } else {
            self.finishiLabel.text = "（未完结）"
        }
        self.toalNum.text = "共\(self.model.totalSeason)集"
        self.listenNum.text = "\(self.model.listenNum)人正在收听"
        self.slider.setThumbImage(UIImage(named: "icon_slider"), for: .normal)
    }
    
    func onPlaying() {
        if onPlay {
            self.playBtn.setImage(IMAGE(name: "listen_delay"), for: .normal)
//            self.hiddenView.isHidden = false
            TTAudioPlayer.sesionId = self.model.currentSeason
            TTAudioPlayer.indextNum = TTAudioPlayer.sesionId - 1
//            TTAudioPlayer.firstPlay(proId: self.model.id, url: self.model.playUrl ?? "", imageUrl: self.model.shareImageUrl ?? "")
            delegate?.playActionDele(cell: self, paly: true)
        } else {
            self.playBtn.setImage(IMAGE(name: "listen_play"), for: .normal)
//            self.hiddenView.isHidden = true
            TTAudioPlayer.stop()
            delegate?.playActionDele(cell: self, paly: false)
        }
    }
    
    func initOnPlaying() {
        if onPlay {
            self.playBtn.setImage(IMAGE(name: "listen_delay"), for: .normal)
//            self.hiddenView.isHidden = false
   
        } else {
            self.playBtn.setImage(IMAGE(name: "listen_play"), for: .normal)
//            self.hiddenView.isHidden = true
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
